
package com.breaktian.demoapp.toobar;



import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.breaktian.demoapp.R;


/**
 * Created by breaktian on 2016/8/17. 之所以添加这个类，主要是方便使用，屏蔽ToolBar使用中的各种问题
 */
public class ToolBarBuilder {
    private Toolbar toolbar;
    /** 标题 */
    private String title;

    /** 是否要隐藏右侧 */
    private boolean hideRight = true;

    /** 右侧文字*/
    private String rightStr;
    /** 右侧图标*/
    private int rightIcon = -1;

    /** NavigationIcon点击事件监听（返回键监听） */
    private View.OnClickListener rightClickListener;



    /** 是否要隐藏返回键 */
    private boolean hideBackIcon = false;
    /** 返回键图标 */
    private int navigationIconId = R.drawable.iv_back;
    /** NavigationIcon点击事件监听（返回键监听） */
    private View.OnClickListener navigationClickListener;
    /** Menu响应事件监听 */
    private Toolbar.OnMenuItemClickListener menuItemClickListener;

    public ToolBarBuilder setToolbar(Toolbar toolbar) {
        this.toolbar = toolbar;
        return this;
    }

    /** 标题 */
    public ToolBarBuilder setTitle(String title) {
        this.title = title;
        return this;
    }


    /** 返回键图标设置 */
    public ToolBarBuilder setNavigationIconId(int navigationIconId) {
        this.navigationIconId = navigationIconId;
        return this;
    }

    /** NavigationIcon点击事件监听（返回键监听） */
    public ToolBarBuilder setNavigationClickListener(View.OnClickListener navigationClickListener) {
        this.navigationClickListener = navigationClickListener;
        return this;
    }

    public ToolBarBuilder setRightClickListener(View.OnClickListener rightClickListener){
        this.rightClickListener = rightClickListener;
        return this;
    }
    public ToolBarBuilder setRightStr(String rightStr){
        this.rightStr = rightStr;
        return this;
    }
    public ToolBarBuilder setRightIcon(int rightIcon){
        this.rightIcon = rightIcon;
        return this;
    }
    public ToolBarBuilder hideRight(boolean hide){
        this.hideRight = hide;
        return this;
    }



    /** Menu Item 点击事件监听（返回键监听） */
    public ToolBarBuilder setMenuItemClickListener(
            Toolbar.OnMenuItemClickListener menuItemClickListener) {
        this.menuItemClickListener = menuItemClickListener;
        return this;
    }



    /** 是否要隐藏返回键 */
    public ToolBarBuilder hideBackIcon(boolean hideBackIcon) {
        this.hideBackIcon = hideBackIcon;
        return this;
    }

    public Toolbar build(final AppCompatActivity activity) {
        if (toolbar != null) {
            if (!hideBackIcon) {// 在不隐藏返回键的情况下要显示返回键
                toolbar.setNavigationIcon(navigationIconId);
            }
            // 这个属性的配置要写在 setSupportActionBar（）之前才会有效果
            toolbar.setTitle("");
            TextView tvTitle = (TextView) activity.findViewById(R.id.tvToolBarTitle);
            tvTitle.setText(title);
            // 设置返回键监听事件
            if (navigationClickListener == null) {
                navigationClickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        activity.finish();
                    }
                };
            }

            TextView tvRight = (TextView) activity.findViewById(R.id.tv_toolbar_right);
            if(!hideRight){
                if(!TextUtils.isEmpty(rightStr)){
                    tvRight.setText(rightStr);
                }
                if(rightIcon!=-1){
                    Drawable drawable = activity.getResources().getDrawable(rightIcon);
                    drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
                    tvRight.setCompoundDrawables(null,null,drawable,null);
                }
                if(rightClickListener!=null){
                    tvRight.setOnClickListener(rightClickListener);
                }
            }else{
                tvRight.setVisibility(View.GONE);
            }

            // 设置Toolbar为导航栏
            activity.setSupportActionBar(toolbar);
            // 这个配置必须写在setSupportActionBar（）之后才会有效果。
            toolbar.setNavigationOnClickListener(navigationClickListener);
            // 这个配置必须写在setSupportActionBar（）之后才会有效果。
            if (menuItemClickListener != null) {
                toolbar.setOnMenuItemClickListener(menuItemClickListener);
            }
        }
        return toolbar;
    }// end build

}// end ToolBarBuilder
